import 'package:dio/dio.dart';
// import 'package:get/get.dart';
import 'package:mall/constant/app_strings.dart';
import 'package:mall/router/navigator_util.dart';
import 'package:mall/utils/hud/hud_util.dart';
import 'package:mall/utils/log/log_manager.dart';
import 'package:mall/utils/shared_preferences_util.dart';

class HttpUtil {
  // 工厂模式
  static HttpUtil get instance => _getInstance();
  static HttpUtil? _httpUtil;
  Dio dio = Dio();

  static HttpUtil _getInstance() {
    if (_httpUtil == null) {
      _httpUtil = HttpUtil();
    }
    return _httpUtil!;
  }

  HttpUtil() {
    BaseOptions options = BaseOptions(
      connectTimeout: Duration(milliseconds: 10000),
      receiveTimeout: Duration(milliseconds: 10000),
    );
    // dio = new Dio(options);
    dio.options = options;

    //添加拦截器
    dio.interceptors
        .add(InterceptorsWrapper(onRequest: (RequestOptions options, RequestInterceptorHandler handler) async {
      LogM.log("========================请求数据===================");
      LogM.log("url=${options.uri.toString()}");
      LogM.log("params=${options.data}");
      // dio.lock();
      //如果token存在在请求头加上token

      final token = await SharedPreferencesUtil.getInstance().getString(AppStrings.TOKEN);
      LogM.log("token=${token}");
      if (token != null) {
        options.headers[AppStrings.TOKEN] = token;
      }
      handler.next(options);
      // dio.unlock();
    }, onResponse: (Response response, handler) {
      LogM.log("========================请求数据===================");
      LogM.log("code=${response.statusCode}");
      LogM.log("response=${response.data}");
      if (response.data[AppStrings.ERR_NO] == 501) {
        // Application.navigatorKey.currentState?.pushNamed(Routers.login);
        // Get.toNamed(Routers.login);
        // dio?.reject("");
        NavigatorUtil.goLogin();
      }
      handler.next(response);
    }, onError: (error, handler) {
      LogM.log("========================请求错误===================");
      LogM.log("message =${error.message}");
      handler.next(error);
    }));
  }

  //get请求
  Future get(String url, {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.get(url, queryParameters: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.get(url, queryParameters: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.get(url, options: options);
    } else {
      response = await dio.get(url);
    }
    var data = response.data;
    if (data[AppStrings.ERR_NO] == 0) {
      return data;
    } else {
      String errorMsg = data[AppStrings.ERR_MSG] != null ? data[AppStrings.ERR_MSG] : AppStrings.SERVER_EXCEPTION;
      HUDUtil.toast(errorMsg);
      throw Exception(errorMsg);
    }
  }

  //post请求
  Future post(String url, {Map<String, dynamic>? parameters, Options? options}) async {
    Response response;
    if (parameters != null && options != null) {
      response = await dio.post(url, data: parameters, options: options);
    } else if (parameters != null && options == null) {
      response = await dio.post(url, data: parameters);
    } else if (parameters == null && options != null) {
      response = await dio.post(url, options: options);
    } else {
      response = await dio.post(url);
    }
    var data = response.data;
    if (data[AppStrings.ERR_NO] == 0) {
      return data;
    } else {
      String errorMsg = data[AppStrings.ERR_MSG] != null ? data[AppStrings.ERR_MSG] : AppStrings.SERVER_EXCEPTION;
      HUDUtil.toast(errorMsg);
      throw Exception(errorMsg);
    }
  }
}
